package cn.cs.controller;

import cn.cs.common.*;
import cn.cs.pojo.CsPerson;
import cn.cs.pojo.CsRoleUser;
import cn.cs.service.CsPersonService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;


@RestController
@RequestMapping(value = "/person")
public class PersonController {

    @Resource
    private CsPersonService csPersonService;

    /**
     * 根据员工Id获取员工信息
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/getPersonById")
    public Dto getPersonById(String id)throws Exception{
        CsPerson person = csPersonService.getCsPersonById(id);
        return DtoUtil.returnSuccess("获取成功", person);
    }

    /**
     * 分页查询 人员档案
     * @param pageNumber
     * @param pageSize
     * @return
     * @throws Exception
     * TODO 添加搜索
     */
    @RequestMapping(value = "/csPlatform/getPersonByPage")
    public Dto getPersonByPage(Integer pageNumber, Integer pageSize, String personName, String personCode, String personIdcard, String personPhone, String personOrgid)throws Exception{
        Map<String, Object> param = new HashMap<>();
        param.put("personName", personName);
        param.put("personCode", personCode);
        param.put("personIdcard", personIdcard);
        param.put("personPhone", personPhone);
        param.put("personOrgid", personOrgid);
        return DtoUtil.returnSuccess("获取成功", csPersonService.queryCsPersonPageByMap(param, pageNumber, pageSize));
    }


    /**
     * 人员档案  -- 根据部门查询人员信息
     * @return
     * @throws Exception
     * TODO 添加搜索
     */
    @RequestMapping(value = "/csPlatform/getPersonListByOrgId")
    public Dto getPersonListByOrgId(String personOrgid)throws Exception{
        Map<String, Object> param = new HashMap<>();
        param.put("personOrgid", personOrgid);
        return DtoUtil.returnSuccess("获取成功", csPersonService.getCsPersonListByMap(param));
    }

    /**
     * 查询所有人员
     * @return
     * @throws Exception
     * TODO 添加搜索
     */
    @RequestMapping(value = "/csPlatform/getPersonAll")
    public Dto getPersonAll()throws Exception{
        Map<String, Object> param = new HashMap<>();
        return DtoUtil.returnSuccess("获取成功", csPersonService.getCsPersonListByMap(param));
    }

    /**
     * 新增/编辑 人员档案
     * @param csPerson
     * @return
     */
    @RequestMapping(value = "/csPlatform/updatePerson")
    public Dto updatePerson(@RequestBody CsPerson csPerson)throws Exception{
            String msg = "新增成功";
            if (EmptyUtils.isEmpty(csPerson.getId())) {
                csPerson.setId(IdWorker.getId());
                csPerson.setPersonCode(PingYinUtil.getFirstSpell(csPerson.getPersonName()) + csPerson.getPersonPhone().substring(7,11));
                csPerson.setPersonPassword(MD5Util.encode(csPerson.getPersonPassword()));
                csPerson.setPersonPasswordModifytime(new Date());
                csPerson.setCreateTime(new Date());
                csPerson.setModifyTime(new Date());
                csPersonService.addCsPerson(csPerson);
            } else {
                if (EmptyUtils.isNotEmpty(csPerson.getPersonPassword())) {
                    csPerson.setPersonPassword(MD5Util.encode(csPerson.getPersonPassword()));
                    csPerson.setPersonPasswordModifytime(new Date());
                }
                csPerson.setModifyTime(new Date());
                msg = "修改成功";
                csPersonService.modifyCsPerson(csPerson);
            }
            return DtoUtil.returnSuccess(msg);
    }

    /**
     * 设置员工是否为系统管理员 / 修改密码
     * @param id
     * @param password
     * @param type 1设置为管理员并设置密码   2设置不是管理员
     * @return
     */
    @RequestMapping(value = "/csPlatform/setPersonPlatform")
    public Dto setPersonPlatform(String id, String password, String type)throws Exception{
            CsPerson csPerson = new CsPerson();
            csPerson.setId(id);
            if("1".equals(type)){
                csPerson.setPersonPassword(MD5Util.encode(password));
                csPerson.setPersonValidState(1);
            }else{
                csPerson.setPersonPassword("null");
                csPerson.setPersonValidState(0);
            }
            csPerson.setPersonPasswordModifytime(new Date());
            csPerson.setModifyTime(new Date());
            csPersonService.modifyCsPerson(csPerson);
            return DtoUtil.returnSuccess("操作成功");

    }


    /**
     * 个人修改自己的 修改密码
     * @param password
     * @return
     */
    @RequestMapping(value = "/csPlatform/setPersonPlatformOwn")
    public Dto setPersonPlatformOwn(String username, String password)throws Exception{
        CsPerson csPerson = csPersonService.findPersonByName(username);
        csPerson.setPersonPassword(MD5Util.encode(password));
        csPerson.setPersonPasswordModifytime(new Date());
        csPerson.setModifyTime(new Date());
        csPersonService.modifyCsPerson(csPerson);
        return DtoUtil.returnSuccess("操作成功");

    }

    /**
     * 给用户分配角色
     * @param id
     * @param roles
     * @return
     */
    @RequestMapping(value = "/csPlatform/setPersonRole")
    public Dto setPersonRole(String id, String roles){
        try {
            List<CsRoleUser> list = new ArrayList<>();
            for(String m:roles.split(",")){
                CsRoleUser csRoleUser = new CsRoleUser();
                csRoleUser.setPersonId(id);
                csRoleUser.setRoleId(m);
                list.add(csRoleUser);
            }
            return DtoUtil.returnSuccess("分配成功", csPersonService.insertPerSonRoleBatch(list, id));
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }


    /**
     * 查看用户角色
     * @param id
     * @return
     */
    @RequestMapping(value = "/csPlatform/getPersonRole")
    public Dto getPersonRole(String id){
        try {
            return DtoUtil.returnSuccess("查询成功", csPersonService.getPersonById(id));
        }catch (Exception e){
            System.out.println(e.getCause().getMessage());
            e.printStackTrace();
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }

    /**
     * 改变员工有效状态 1有效 0失效
     * @param id
     * @param state
     * @return
     */
    @RequestMapping(value = "/csPlatform/updatePersonState")
    public Dto updatePersonState(String id, String state){
        try {
            CsPerson csPerson = new CsPerson();
            csPerson.setId(id);
            csPerson.setPersonValidState(Integer.valueOf(state));
            return DtoUtil.returnSuccess("操作成功", csPersonService.modifyCsPerson(csPerson));
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("操作失败","001");
        }
    }

    /**
     * 删除员工
     * @param ids
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/delPerson")
    public Dto delPerson(String ids)throws Exception{
        int i = 0;
        for(String id:ids.split(",")){
            i += csPersonService.deleteCsPersonById(id);
        }
        if(i>0){
            return DtoUtil.returnSuccess("操作成功", i);
        }else {
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }

    /**
     * 分配人员
     * @param ids
     * @param orgId
     * @return
     */
    @RequestMapping(value = "/csPlatform/distributionPerson")
    @Transactional
    public Dto distributionPerson(String ids, String orgId)throws Exception{
        for(String id:ids.split(",")){
            CsPerson p = new CsPerson();
            p.setId(id);
            p.setPersonOrgid(orgId);
            csPersonService.modifyCsPerson(p);
        }
        return DtoUtil.returnSuccess("操作成功");
    }



}
